﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace search
{
	/// <summary>
	/// Article 的摘要说明。
	/// </summary>
	public partial class Article : System.Web.UI.Page
	{
		private string subject;

	
		protected void Page_Load(object sender, System.EventArgs e)
		{
			// 在此处放置用户代码以初始化页面

			if(Request["id"] == null || Request["id"].ToString() == "")
				return;

			if(!Page.IsPostBack)
			{
				//sp_IncreaseClicks
				//SearchDataAccess.IncreaseClicks(Convert.ToInt64(Request["id"]));

				DataSet ds = SearchDataAccess.GetArticleByPostID(Convert.ToInt64(Request["id"]));
				if(ds.Tables[0].Rows.Count > 0)
				{
					DataRow dr = ds.Tables[0].Rows[0];
					Response.Redirect("../client/bloggercontent.aspx?UserID=" + dr["UserID"].ToString() + "&PostID=" + Request["id"].ToString());

					this.Label_NickName.Text = dr["NickName"].ToString();
					this.Literal_UserArticles.Text = "<a href='AllArticles.aspx?id=" + dr["UserID"].ToString() + "&name=" + Server.UrlEncode(dr["NickName"].ToString()) + "' target=_blank>所有 " + dr["NickName"].ToString() + " 的文章</a>";
					this.Label_Subject.Text = dr["Subject"].ToString();
					subject = dr["Subject"].ToString();

					string chapter = dr["BibleChapter"].ToString();
					string section = dr["BibleSection"].ToString();
					string chapter_to = dr["BibleChapter_To"].ToString();
					string section_to = dr["BibleSection_To"].ToString();
					string scripture = dr["BibleTitle"].ToString() + "&nbsp;&nbsp;" + chapter;
					if(section != "")
						scripture += "∶" + section;
					if(chapter_to != "" || section_to != "")
						scripture += "&nbsp;—&nbsp;";
					if(chapter_to != "")
						scripture += chapter_to;
					if(section_to != "")
						scripture += "∶" + section_to;

					this.Label_Scripture.Text = "<a class=a1 href='BibleTools.aspx?title=" + Server.UrlEncode(dr["BibleTitle"].ToString()) + 
							"&c=" + dr["BibleChapter"].ToString() + "&s=" + dr["BibleSection"].ToString() + 
							"&c1=" + dr["BibleChapter_To"].ToString() + "&s1=" + dr["BibleSection_To"].ToString() + "'>" + scripture + "</a>";

					DataSet dsPostBible = SearchDataAccess.GetPostBibleByPostID(dr["PostID"].ToString());
					foreach(DataRow row in dsPostBible.Tables[0].Rows)
					{
						this.Label_Scripture.Text += "&nbsp;&nbsp;<a class=a1 href='BibleTools.aspx?title=" + Server.UrlEncode(row["BibleTitle"].ToString()) + 
							"&c=" + row["BibleChapter"].ToString() + "&s=" + row["BibleSection"].ToString() + 
							"&c1=" + row["BibleChapter_To"].ToString() + "&s1=" + row["BibleSection_To"].ToString() + "'>" + row["BibleTitle"].ToString() + "&nbsp;" + row["BibleChapter"].ToString() + "∶" + row["BibleSection"].ToString() + "&nbsp;—&nbsp;" + 
							row["BibleChapter_To"].ToString() + "∶" + row["BibleSection_To"].ToString() + "</a>";
					}
					this.Literal_Body.Text = dr["Body"].ToString().Replace("\n", "<br>");
					this.Literal_Body.Text = this.Literal_Body.Text.Replace(" ", "&nbsp;");

					if(dr["Status"] != DBNull.Value)
					{
						this.Literal_Vote.Visible = true;
						this.Button_yes.Visible = true;
						this.Button_no.Visible = true;

						HttpCookie voteCookie = Request.Cookies["VoteArticle" + Request["id"].ToString()];
						if(voteCookie != null)
						{
							if(voteCookie.Value == "1")
							{
								this.Button_yes.Attributes["onclick"] = "alert('在此之前，你已经对本篇文章投了对你“有”帮助一票！'); return false;";
								this.Button_no.Attributes["onclick"] = "alert('在此之前，你已经对本篇文章投了对你“有”帮助一票！'); return false;";
							}
							else
							{
								this.Button_yes.Attributes["onclick"] = "alert('在此之前，你已经对本篇文章投了对你“没有”帮助一票！'); return false;";
								this.Button_no.Attributes["onclick"] = "alert('在此之前，你已经对本篇文章投了对你“没有”帮助一票！'); return false;";
							}
						}
					}
					else
					{
						this.Literal_Vote.Visible = false;
						this.Button_yes.Visible = false;
						this.Button_no.Visible = false;
					}
				}
			}
		}

		#region Web 窗体设计器生成的代码
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// 设计器支持所需的方法 - 不要使用代码编辑器修改
		/// 此方法的内容。
		/// </summary>
		private void InitializeComponent()
		{    

		}
		#endregion

		protected void Button_yes_Click(object sender, System.EventArgs e)
		{
			HttpCookie voteCookie = Request.Cookies["VoteArticle" + Request["id"].ToString()];
			if(voteCookie == null)
			{
				SearchDataAccess.VoteArticle(Convert.ToInt64(Request["id"]), "1");
				voteCookie = new HttpCookie("VoteArticle" + Request["id"].ToString(), "1");
				voteCookie.Expires = DateTime.MaxValue;
				Response.Cookies.Add(voteCookie);
				this.Button_yes.Attributes["onclick"] = "alert('在此之前，你已经对本篇文章投了对你“有”帮助一票！'); return false;";
				this.Button_no.Attributes["onclick"] = "alert('在此之前，你已经对本篇文章投了对你“有”帮助一票！'); return false;";
			}
		}

		protected void Button_no_Click(object sender, System.EventArgs e)
		{
			HttpCookie voteCookie = Request.Cookies["VoteArticle" + Request["id"].ToString()];
			if(voteCookie == null)
			{
				SearchDataAccess.VoteArticle(Convert.ToInt64(Request["id"]), "0");
				voteCookie = new HttpCookie("VoteArticle" + Request["id"].ToString(), "0");
				voteCookie.Expires = DateTime.MaxValue;
				Response.Cookies.Add(voteCookie);
				this.Button_yes.Attributes["onclick"] = "alert('在此之前，你已经对本篇文章投了对你“没有”帮助一票！'); return false;";
				this.Button_no.Attributes["onclick"] = "alert('在此之前，你已经对本篇文章投了对你“没有”帮助一票！'); return false;";
			}
		}

		public string Subject()
		{
			return subject;
		}
	}
}
